package de.jare.photoswapper;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.io.OutputStreamWriter; 
import java.net.Socket; 
import java.net.UnknownHostException;

import android.app.ProgressDialog;
import android.util.Log;

public class HttpHandler {
	
	private String host = "swap.ganzbunt.de";

	public static String AndroidID;
	public static String MobileNr;
	public static String DeviceID;

	private ProgressDialog pdlg = null;
	
	public void UploadPicture(final byte[] data, Preview prv) {
		try
		{
					
					Log.d("PS", "HttpHandler.UploadPicture(): Connecting to " + host);
					
					Socket socket = new java.net.Socket(host, 80);
					
					DataOutputStream  os = new DataOutputStream(socket.getOutputStream());
					Writer wr = new OutputStreamWriter(os);
					
					String multipart2;
					multipart2 = "-----------------------------29772313742745\r\n"
						+ "Content-Disposition: form-data; name=\"photoswapper\"; filename=\"upload.jpg\"\r\n"
						+ "Content-Type: image/jpeg\r\n\r\n";
					
					String multipart1;
					multipart1 = "-----------------------------29772313742745\r\n"
						+ "Content-Disposition: form-data; name=\"MAX_FILE_SIZE\"\r\n"
						+ "\r\n5000000\r\n";
					
					String trail;
					
					trail = "\r\n-----------------------------29772313742745--\r\n";
					
					String header;
					header = "POST /put.php HTTP/1.1\r\n"
						+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
						+ "Referer: http://localhost\r\n"
						+ "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n"
						+ "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
						+ "Content-Type: multipart/form-data; boundary=---------------------------29772313742745\r\n"
						+ "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)\r\n"
						+ "Host: " + host + "\r\n"
						+ "Content-length: " + ((int)data.length + (int)multipart1.length() + (int)multipart2.length() + (int)trail.length()) + "\r\n"
						+ "Keep-Alive: 300\r\n"
						+ "Connection: Keep-Alive\r\n"
						+ "Pragma: no-cache\r\n"
						+ "AndroidId: " + AndroidID + "\r\n"
						+ "IMEI: " + DeviceID + "\r\n"
						+ "MobileNr: " + MobileNr + "\r\n"
						+ "\r\n";
					
					wr.write(header);
					wr.write(multipart1);
					wr.write(multipart2);
					wr.flush();
					
					os.write(data);
					os.flush();
					
					wr.write(trail);
					wr.flush();
					
					Log.d("PS", "HttpHandler.UploadPicture(): flushing");
					
					
				} catch (UnknownHostException e) {
					Log.d("PS", "HttpHandler.UploadPicture(): IOException " + e.getMessage());
					e.printStackTrace();
				} catch (IOException e) {
					Log.d("PS", "HttpHandler.UploadPicture(): UnknownHostException " + e.getMessage());
					e.printStackTrace();
				}
	}
	
	public byte[] ReceivePicture() {
		byte[] image = null;
		
		try {
			
			Log.d("PS", "HttpHandler.ReceivePicture(): Connecting to " + host);
			
			Socket socket = new java.net.Socket(host, 80);
			
			DataOutputStream os = new DataOutputStream(socket.getOutputStream());
			
			
			InputStream is = socket.getInputStream();
			//InputStream is = new BufferedInputStream(socket.getInputStream());
			//BufferedInputStream is = new BufferedInputStream(socket.getInputStream());
		

			
			
			/* Write the http request */
			Writer wr = new OutputStreamWriter(os);
			
			String header;
			header = "GET /get.php HTTP/1.1\r\n"
				+ "Accept: image/jpeg\r\n" + "Referer: http://localhost\r\n"
				+ "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n"
				+ "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
				+ "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)\r\n"
				+ "Host: " + host + "\r\n"
				+ "Keep-Alive: 300\r\n"
				+ "Connection: Keep-Alive\r\n"
				+ "Pragma: no-cache\r\n"
				+ "AndroidId: " + AndroidID + "\r\n"
				+ "IMEI: " + DeviceID + "\r\n"
				+ "MobileNr: " + MobileNr + "\r\n"
				+ "\r\n";
			
			wr.write(header);
			wr.flush();
			
			/* Read the http response */
			
			byte[] userInput = new byte[5000000];
			int crCounter = 0;
			int singleByte;
			int i = 0;
			
			while ((singleByte = is.read()) != -1) {
			    userInput[i] = (byte)singleByte;
			    i++;
			}
			
			int a, b = 0;
			
			
			for (a = 0; a < i; a++) {
				if ((int)userInput[a] == 10) {
					crCounter++;
				}
				if (crCounter == 9) {
					b = a + 1;
					break;
				}
				
			}
			image = new byte[i - a - 1];
			
			
			for (int c = b; c < (i - 1); c++)
			{
				image[c - b] = (byte)userInput[c];
			}
			
			Log.d("PS", "len " + image.length);
			
			
		} catch (UnknownHostException e) {
			Log.d("PS", "HttpHandler.UploadPicture(): IOException " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("PS", "HttpHandler.UploadPicture(): UnknownHostException " + e.getMessage());
			e.printStackTrace();
		}
		return image;
	}
	
	public void RegisterDevice() {
		try {
			
			Log.d("PS", "HttpHandler.ReceivePicture(): Connecting to " + host);
			
			Socket socket = new java.net.Socket(host, 80);
			
			DataOutputStream os = new DataOutputStream(socket.getOutputStream());
			
			
			//InputStream is = socket.getInputStream();
			InputStream is = new BufferedInputStream(socket.getInputStream());
			//BufferedInputStream is = new BufferedInputStream(socket.getInputStream());
			
			
			/* Write the http request */
			Writer wr = new OutputStreamWriter(os);
			
			String header;
			header = "GET /register.php HTTP/1.1\r\n"
				+ "Accept: image/jpeg\r\n" + "Referer: http://localhost\r\n"
				+ "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n"
				+ "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
				+ "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)\r\n"
				+ "Host: " + host + "\r\n"
				+ "Keep-Alive: 300\r\n"
				+ "Connection: Keep-Alive\r\n"
				+ "Pragma: no-cache\r\n"
				+ "AndroidId: " + AndroidID + "\r\n"
				+ "IMEI: " + DeviceID + "\r\n"
				+ "MobileNr: " + MobileNr + "\r\n"
				+ "\r\n";
			
			wr.write(header);
			wr.flush();
			
			/* Read the http response */
			
			byte[] userInput = new byte[5000000];
			
			int singleByte;
			int i = 0;
			while ((singleByte = is.read()) != -1) {
			    userInput[i] = (byte)singleByte;
			    i++;
			    //Log.d("PS", "Read stream. i = " + i);
			}
			Log.d("PS", "Received bytes. length = " + userInput.length);
			
			
		} catch (UnknownHostException e) {
			Log.d("PS", "HttpHandler.UploadPicture(): IOException " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("PS", "HttpHandler.UploadPicture(): UnknownHostException " + e.getMessage());
			e.printStackTrace();
		}
	}
}
